package cc.vv.baselibrary.fragment;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.VideoView;

import cc.vv.baselibrary.R;
import cc.vv.baselibrary.bean.PublicViewHolder;

/**
 * Created by sun.li on 2018/10/18.
 *
 * @author sun.li
 */

public class BaseVideoPlayerFragment extends BaseFragment{

    public static final String VideoPathLogo = "VideoPathLogo";

    private String videoPath = "";

    /**
     * 视频是否准备好
     */
    private boolean videoReadiness = false;

    private VideoView vv_video;

    private ProgressBar pb_bvpa_video_loading;

    private ImageView iv_bvpa_video_start;

    /**
     * 两次单击的时间间隔
     */
    private static final int DOUBLE_CLICK_TIME = 350;

    private boolean waitDouble = true;

    private MediaController mediaController;

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_base_video_player;
    }

    @Override
    protected PublicViewHolder initViewHolderObject() {
        return null;
    }

    @Override
    protected void baseOnClick(int viewId) {

    }

    @Override
    protected void initView(Bundle bundle) {
        super.initView(bundle);
        try {
            videoPath = getActivity().getIntent().getStringExtra(VideoPathLogo);
        } catch (Exception e) {
            e.printStackTrace();
            videoPath = "";
        }
        vv_video = getActivity().findViewById(R.id.vv_bvpa_video_player);
        pb_bvpa_video_loading = getActivity().findViewById(R.id.pb_bvpa_video_loading);
        iv_bvpa_video_start = getActivity().findViewById(R.id.iv_bvpa_video_start);
        initVideoView();
    }

    public void setVideoPath(String videoPath) {
        this.videoPath = videoPath;
        initVideoView();
    }

    /**
     *初始化videoview播放
     */
    public void initVideoView(){
        if (null != videoPath && !videoPath.equals("") && null!=vv_video) {
            Uri videoUri = Uri.parse(videoPath);
            if(null == videoUri){
                return;
            }
            vv_video.setVideoURI(videoUri);

            //初始化videoview控制条
            mediaController=new MediaController(getActivity());
            //设置videoview的控制条
            vv_video.setMediaController(mediaController);
            //设置显示控制条
            mediaController.show(0);

            // 播放视频
            videoPlay();

            if(Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN){
                vv_video.setOnInfoListener(new MediaPlayer.OnInfoListener(){

                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                        if(what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START){
                            iv_bvpa_video_start.setVisibility(View.GONE);
                        }else if(what == MediaPlayer.MEDIA_INFO_BUFFERING_START){
                            pb_bvpa_video_loading.setVisibility(View.VISIBLE);
                        }else if(what == MediaPlayer.MEDIA_INFO_BUFFERING_END){
                            //此接口每次回调完START就回调END,若不加上判断就会出现缓冲图标一闪一闪的卡顿现象
                            if(mp.isPlaying()){
                                pb_bvpa_video_loading.setVisibility(View.GONE);
                            }
                        }
                        return true;
                    }
                });
            }

            vv_video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    iv_bvpa_video_start.setVisibility(View.GONE);
                    pb_bvpa_video_loading.setVisibility(View.GONE);
                    videoReadiness = true;
                }
            });

            vv_video.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    pb_bvpa_video_loading.setVisibility(View.GONE);
                    iv_bvpa_video_start.setVisibility(View.VISIBLE);
                    return false;
                }
            });

            vv_video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    pb_bvpa_video_loading.setVisibility(View.GONE);
                    iv_bvpa_video_start.setVisibility(View.VISIBLE);
                }
            });

            iv_bvpa_video_start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    videoPlay();
                }
            });

            vv_video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (waitDouble == true) {
                        waitDouble = false;
                        Thread thread = new Thread() {
                            @Override
                            public void run() {
                                try {
                                    sleep(DOUBLE_CLICK_TIME);
                                    if (waitDouble == false) {
                                        waitDouble = true;
                                        singleClick();
                                    }
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        };
                        thread.start();
                    } else {
                        waitDouble = true;
                        doubleClick();
                    }
                }
            });
        }
    }

    private void singleClick() {
        System.out.println("single");
    }

    private void doubleClick() {
        System.out.println("double");
        videoStop();
    }

    /**
     * 视频播放
     */
    private void videoPlay() {
        vv_video.start();
        if (!videoReadiness) {
            pb_bvpa_video_loading.setVisibility(View.VISIBLE);
        }
        iv_bvpa_video_start.setVisibility(View.GONE);
    }

    /**
     * 视频暂停
     */
    private void videoStop() {
        if(null!=vv_video){
            vv_video.pause();
            iv_bvpa_video_start.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        vv_video.pause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
                if (null != vv_video) {
            vv_video.stopPlayback();
            vv_video = null;
        }
        try {
            System.gc();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
